package com.csii.user.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 规避中等级缺陷：HTTP参数污染
 */
@Slf4j
@Aspect
@Component
public class DeInterceptor {

    @Pointcut("execution(* com.csii.user.rest..*.refreshToken(..))")
    public void refreshToken() {
    }

    @Pointcut("execution(* com.csii.user.rest..*.checkToken(..))")
    public void checkToken() {
    }

    @Around("refreshToken() && checkToken()")
    public Object interceptor(ProceedingJoinPoint point) throws Throwable {
        // 获取请求参数
        Object[] args = point.getArgs();
        String reqestParam = Arrays.toString(args);
        log.info("interceptor-入参：{}", reqestParam);
        // 处理参数
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof String) {
                args[i] = ((String) args[i]).replaceAll("&", "");
            }
        }
        // 获取响应数据
        return point.proceed(args);
    }

}